//
//  RequestGenerator.m
//  YiJuZhangShou
//
//  Created by 冷悦 on 16/3/3.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "RequestGenerator.h"
#import "AppVersion.h"
#import "NetworkingHeader.h"
#import "AppContext.h"

@interface RequestGenerator ()

@property (atomic, strong)NSMutableArray *requests; //正在请求的request数组

@property (atomic, strong)NSMutableDictionary *cachedDatas; //缓存的request结果

@end

static RequestGenerator *_instance = nil;

@implementation RequestGenerator

+ (instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _instance = [[RequestGenerator alloc]init];
    });
    
    return _instance;
}

- (instancetype)init{
    self = [super init];
    
    if (self != nil) {
        self.requests = [NSMutableArray array];
        self.cachedDatas = [NSMutableDictionary dictionary];
    }
    
    return self;
}

#pragma mark - EventResponse
- (void)timerAction:(NSTimer *)timer{
    NSString *requestId = timer.userInfo;
    if (requestId != nil) {
        [self.cachedDatas removeObjectForKey:requestId];
    }
    
    [timer invalidate];
}

#pragma mark - Public
- (NSString *)generateApiUrl:(NSString *)baseUrl
              withApiVersion:(NSString *)apiVersion
                  andName:(NSString *)name{
    NSString *baseUrlNew = baseUrl == nil ? [AppContext sharedInstance].baseUrl : baseUrl;
    NSString *apiVersionNew = apiVersion == nil ? kApiVersion : apiVersion;
    NSString *apiNameNew = name == nil ? @"" : name;
    NSString *api = [NSString stringWithFormat:@"%@/%@/%@", baseUrlNew, apiVersionNew, apiNameNew, nil];
    
    return api;
}

- (NSString *)generateCommonApiWithName:(NSString *)name{
    AppContext *context = [AppContext sharedInstance];
    NSString *apiName = [self generateApiUrl:context.baseUrl
                              withApiVersion:nil
                                     andName:name];
    
    return apiName;
}

- (void)deleteProcessingRequest:(NSString *)requestId{
    [self.requests removeObject:requestId];
}

- (void)addProcessingRequest:(NSString *)requestId{
    if ([self isRequestOnProcessing:requestId] == NO) {
        [self.requests addObject:requestId];
    }
}

- (BOOL)isRequestOnProcessing:(NSString *)requestId{
    return [self.requests containsObject:requestId];
}

- (BOOL)isRequestCached:(NSString *)requestId{
    return [[self.cachedDatas allKeys] containsObject:requestId];
}



- (void)cacheResponseResult:(NSDictionary *)responseParam
              withRequestId:(NSString *)requestId
               AndCacheTime:(NSTimeInterval)cacheTime{
    if (cacheTime > 0) {
        if ([self isRequestCached:requestId] == NO) {
            [self.cachedDatas setObject:responseParam forKey:requestId];
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:cacheTime
                                             target:self
                                           selector:@selector(timerAction:)
                                           userInfo:requestId
                                            repeats:NO];
            [timer fire];
        }
    }
}

- (NSString *)generateRequestId:(NSDictionary *)params
                    requestType:(RequestType)requestType
                     andAPIName:(NSString *)apiName{
    NSString *requestId = [NSString stringWithFormat:@"%lu%@%@", (unsigned long)requestType, apiName, params];
    
    return requestId;
}

@end
